#include "accountmanageview.h"
#include "ui_accountmanageview.h"
#include "dialog.h"
#include "createaccountdialog.h"
#include "verifyruleview.h"
#include "changepasswordview.h"
#include <QDebug>

AccountManageView::AccountManageView(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AccountManageView)
{
    ui->setupUi(this);
    setWindowTitle("用户管理");
}

AccountManageView::~AccountManageView()
{
    delete ui;
}

void AccountManageView::insertWidgetItem(int row)
{
    QString style = "\
                    QPushButton{\
                        background-color: rgb(255, 255, 255);\
                        border: 1px solid rgb(0, 0, 0);\
                        height: 26px;\
                        border-radius: 5px;}\
                    QPushButton:hover{\
                        background-color: rgb(220, 220, 220);}";
    if (rule == 1)
    {
        QTableWidgetItem *item  = new QTableWidgetItem();
        ui->tableWidget->setItem(row, 2, item);
        QPushButton *btn = new QPushButton();
        if (accountsInfo.at(row).uuid == uuid)
        {
            btn->setText("重置密码");
        }
        else
        {
            btn->setText("复位密码");
        }
        btn->setStyleSheet(style);
        ui->tableWidget->setCellWidget(row, 2, btn);
        connect(btn, &QPushButton::clicked, this, &AccountManageView::clickedButton);
    }
    else if (rule == 0)
    {
        QTableWidgetItem *item  = new QTableWidgetItem();
        if (accountsInfo.at(row).uuid == uuid)
        {
            ui->tableWidget->setItem(row, 2, item);
            QPushButton *btn = new QPushButton();
            btn->setText("重置密码");
            btn->setStyleSheet(style);
            ui->tableWidget->setCellWidget(row, 2, btn);
            connect(btn, &QPushButton::clicked, this, &AccountManageView::clickedButton);
        }
        else
        {
            item->setFlags(item->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);
            ui->tableWidget->setItem(row, 2, item);
        }
    }
}

void AccountManageView::showEvent(QShowEvent *)//显示时更新数据
{
    initData();
}

void AccountManageView::initData()
{
    DBOperate db;
    accountsInfo = db.selectAccountsInfo();
    ui->tableWidget->setColumnCount(3);
    QStringList hHeaderLabels;
    hHeaderLabels<<"用户名"<<"角色"<<"";
    ui->tableWidget->setHorizontalHeaderLabels(hHeaderLabels);
    ui->tableWidget->horizontalHeader()->setStyleSheet("\
    QHeaderView::section{\
        background-color: rgb(200, 255, 255);\
        border: none;height:28px;}");
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->verticalHeader()->setHidden(true);
    if (rule == Rule::Administrator)
    {
        ui->tableWidget->setRowCount(accountsInfo.length());
        for (int i = 0; i < accountsInfo.length(); i++)
        {
            QTableWidgetItem *item_1  = new QTableWidgetItem();
            item_1->setText(accountsInfo.at(i).userName);
            item_1->setFlags(item_1->flags() & ~Qt::ItemIsEditable);
            QTableWidgetItem *item_2  = new QTableWidgetItem();
            item_2->setFlags(item_2->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);
            if (accountsInfo.at(i).rule == 0)
            {
                item_2->setText("值班员");
            }
            else if (accountsInfo.at(i).rule == 1)
            {
                item_2->setText("管理员");
            }
            ui->tableWidget->setItem(i, 0, item_1);
            ui->tableWidget->setItem(i, 1, item_2);
            insertWidgetItem(i);
        }
    }
    else if (rule == Rule::Ordinary)
    {
        for (int i = 0; i < accountsInfo.length(); i++)
        {
            if (accountsInfo.at(i).rule == Rule::Administrator)
            {
                accountsInfo.remove(i);
                continue;
            }
        }
        ui->tableWidget->setRowCount(accountsInfo.length());
        for (int i = 0; i < accountsInfo.length(); i++)
        {
            QTableWidgetItem *item_1  = new QTableWidgetItem();
            item_1->setText(accountsInfo.at(i).userName);
            item_1->setFlags(item_1->flags() & ~Qt::ItemIsEditable);
            ui->tableWidget->setItem(i, 0, item_1);
            if (accountsInfo.at(i).uuid == uuid)
            {
                QTableWidgetItem *item_2  = new QTableWidgetItem();
                item_2->setFlags(item_2->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);
                item_2->setText("值班员");
                ui->tableWidget->setItem(i, 1, item_2);
                insertWidgetItem(i);
            }
            else
            {
                QTableWidgetItem *item_2  = new QTableWidgetItem();
                item_2->setFlags(item_2->flags() & ~Qt::ItemIsEnabled & ~Qt::ItemIsSelectable);
                item_2->setText("");
                ui->tableWidget->setItem(i, 1, item_2);
                insertWidgetItem(i);
            }
        }
    }
}

void AccountManageView::clickedButton()
{
    QPushButton *senderObj=qobject_cast<QPushButton*>(sender());
    if(senderObj == nullptr)
    {
        return;
    }
    QModelIndex idx =ui->tableWidget->indexAt(QPoint(senderObj->frameGeometry().x(),senderObj->frameGeometry().y()));
    int row=idx.row();
    VerifyRuleView verifyRuleView(this);
    verifyRuleView.setModal(true);
    verifyRuleView.setUserName(accountsInfo.at(row).userName);
    verifyRuleView.show();
    if (verifyRuleView.exec() != QDialog::Accepted)
    {
        Rule getRule = verifyRuleView.getUserRule();
        if (accountsInfo.at(row).uuid == uuid && getRule != Rule::NotLogin)
        {
            ChangePasswordView changePasswordView(this);
            changePasswordView.setModal(true);
            changePasswordView.setID(uuid);
            changePasswordView.show();
            if (changePasswordView.exec() != QDialog::Accepted)
            {
                if (!changePasswordView.getSuccessState())
                {
                    return;
                }
            }
        }
        else if (getRule == 1)
        {
            Dialog dialog(this);
            dialog.setModal(true);
            dialog.setMessage(QString("是否复位用户“%1”的密码？").arg(accountsInfo.at(row).userName));
            dialog.show();
            if (dialog.exec() == QDialog::Accepted)
            {
                DBOperate db;
                db.changePassword(accountsInfo.at(row).uuid, "88888888");
                db.changeFirstLoginState(accountsInfo.at(row).uuid, 0);
            }
        }
    }
}

void AccountManageView::on_pushButton_2_clicked()
{
    VerifyRuleView verifyRuleView(this);
    verifyRuleView.setModal(true);
    verifyRuleView.show();
    if (verifyRuleView.exec() != QDialog::Accepted)
    {
        if (verifyRuleView.getUserRule() != Rule::Administrator)
        {
            return;
        }
        int currentRow = ui->tableWidget->currentRow();
        if (currentRow < 0 || currentRow >= ui->tableWidget->rowCount())
        {
            return;
        }
        AccountInfo accountInfo = accountsInfo.at(currentRow);
        if (accountInfo.rule == 1)
        {
            return;
        }
        Dialog dialog(this);
        dialog.setModal(true);
        dialog.setMessage(QString("是否删除用户“%1”？").arg(accountInfo.userName));
        dialog.show();
        if (dialog.exec() == QDialog::Accepted)
        {
            DBOperate db;
            if (db.deleteAccount(accountInfo.uuid))
            {
                accountsInfo.remove(currentRow);
                ui->tableWidget->removeRow(currentRow);
            }
        }
    }
}

void AccountManageView::on_pushButton_clicked()
{
    VerifyRuleView verifyRuleView(this);
    verifyRuleView.setModal(true);
    verifyRuleView.show();
    if (verifyRuleView.exec() != QDialog::Accepted)
    {
        if (verifyRuleView.getUserRule() != Rule::Administrator)
        {
            return;
        }
        CreateAccountDialog dialog(this);
        dialog.setModal(true);
        dialog.show();
        if (dialog.exec() == QDialog::Accepted)
        {
            QString userName = dialog.getUserName();
            if (!userName.isEmpty() && userName.length() <= 12)
            {
                DBOperate db;
                if (db.createAccount(dialog.getUserName()))
                {
                    initData();
                }
            }
        }
    }
}

void AccountManageView::setRule(int uuid, Rule rule)
{
    this->uuid = uuid;
    this->rule = rule;
}
